package com.pkk.blny.service.impl;

import com.pkk.blny.model.domain.Gallery;
import com.pkk.blny.repository.GalleryRepository;
import com.pkk.blny.service.GalleryService;
import com.pkk.blny.model.domain.Gallery;
import com.pkk.blny.repository.GalleryRepository;
import java.util.List;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

/**
 * <pre>
 *     图库业务逻辑实现类
 * </pre>
 *
 * @author : RYAN0UP
 * @date : 2018/2/26
 */
@Service
public class GalleryServiceImpl implements GalleryService {

  private static final String GALLERIES_CACHE_NAME = "galleries";

  @Autowired
  private GalleryRepository galleryRepository;

  /**
   * 保存图片
   *
   * @param gallery gallery
   * @return Gallery
   */
  @Override
  @CacheEvict(value = GALLERIES_CACHE_NAME, allEntries = true, beforeInvocation = true)
  public Gallery save(Gallery gallery) {
    return galleryRepository.save(gallery);
  }

  /**
   * 根据编号删除图片
   *
   * @param galleryId galleryId
   * @return Gallery
   */
  @Override
  @CacheEvict(value = GALLERIES_CACHE_NAME, allEntries = true, beforeInvocation = true)
  public Gallery remove(Long galleryId) {
    Optional<Gallery> gallery = this.findByGalleryId(galleryId);
    galleryRepository.delete(gallery.get());
    return gallery.get();
  }

  /**
   * 查询所有图片 分页
   *
   * @param pageable pageable
   * @return Page
   */
  @Override
  public Page<Gallery> findAll(Pageable pageable) {
    return galleryRepository.findAll(pageable);
  }

  /**
   * 查询所有图片 不分页
   *
   * @return List
   */
  @Override
  @Cacheable(value = GALLERIES_CACHE_NAME, key = "'gallery'")
  public List<Gallery> findAll() {
    return galleryRepository.findAll();
  }

  /**
   * 根据编号查询图片信息
   *
   * @param galleryId galleryId
   * @return Optional
   */
  @Override
  public Optional<Gallery> findByGalleryId(Long galleryId) {
    return galleryRepository.findById(galleryId);
  }
}
